package com.superzqb.asus.superzqbFragment.superzqbJobShop.Enterprise;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.baoyz.widget.PullRefreshLayout;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.superzqb.asus.Api.CampusShopApi;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Adapter.ConcurrentStudentAdapter;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Bean.CampusEnterpriseListBean;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Bean.CampusShopAllBean;
import com.superzqb.asus.superzqbUtil.GeneralFun;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static com.superzqb.asus.Api.ApiParam.imgUrl;
import static com.superzqb.asus.Api.ApiParam.maxItem;

public class ConcurrentStudent extends Activity implements View.OnClickListener{
    private Context context;
    private ImageButton back;
    private ListView listView;
    private PullRefreshLayout layout;
    private Spinner areaArray,type,area;
    private TextView request;
    private TextView enterprise,mine;
    private ArrayAdapter<String> areaArrayAdapter,typeAdapter,areaAdapter;
    private String[] areaArrayItems,typeItems,areaItems;
    private ConvenientBanner convenient;//顶部广告栏控件
    private LinearLayout li;
    private ViewGroup.LayoutParams i;
    private int lastItem;
    private int listSize;
    private int pagenum=1;
    private boolean isButtom=false;
    private List<CampusEnterpriseListBean> listAll= new ArrayList<>();
    private ConcurrentStudentAdapter adapter;
    public static  List<CampusEnterpriseListBean> list = new ArrayList<>();
    private String[] images = {"company1.jpg","company2.jpg","company3.jpg","company4.jpg"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.concurrent_student);
        initView();
        bindData();
        for(int i=0;i<4;i++){
            images[i]=imgUrl+images[i];
        }
        adapter=new ConcurrentStudentAdapter(context,list);
        listView.setAdapter(adapter);
        new GeneralFun().setCarousel(ConcurrentStudent.this,convenient,images,i.width,i.height);
        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.setRefreshing(false);
                        mClear();
                        getLv();
                    }
                }, 2000);
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(context,CampusPositionInformation.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("item",(CampusEnterpriseListBean)parent.getItemAtPosition(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
    private void bindData() {
        areaArrayAdapter=new ArrayAdapter<>(this,R.layout.simple_spinner_item, areaArrayItems);
        areaArrayAdapter.setDropDownViewResource(R.layout.drop_down_item);
//绑定 Adapter到控件
        areaArray .setAdapter(areaArrayAdapter);
        areaArray.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               // Toast.makeText(EnterpriseStudent.this, "您点击的是"+areaArrayItems[position], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        typeAdapter=new ArrayAdapter<>(this,R.layout.simple_spinner_item, typeItems);
        typeAdapter.setDropDownViewResource(R.layout.drop_down_item);
//绑定 Adapter到控件
        type .setAdapter(typeAdapter);
        type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(EnterpriseStudent.this, "您点击的是"+typeItems[position], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        areaAdapter=new ArrayAdapter<>(this,R.layout.simple_spinner_item, areaItems);
        areaAdapter.setDropDownViewResource(R.layout.drop_down_item);
//绑定 Adapter到控件
        area .setAdapter(areaAdapter);
        area.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              //  Toast.makeText(EnterpriseStudent.this, "您点击的是"+areaItems[position], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void initView() {
        context=ConcurrentStudent.this;
        back=(ImageButton) findViewById(R.id.back);
        areaArray = (Spinner) findViewById(R.id.area_array);
        type = (Spinner) findViewById(R.id.type);
        area = (Spinner) findViewById(R.id.area);
        request = (TextView) findViewById(R.id.request);
        layout= (PullRefreshLayout)findViewById(R.id.swipeRefreshLayout);
        listView=(ListView)findViewById(R.id.listView);

        li=(LinearLayout)findViewById(R.id.play);
        convenient = (ConvenientBanner)findViewById(R.id.convenientBanner);

        enterprise=(TextView)findViewById(R.id.enterprise);
        mine=(TextView)findViewById(R.id.mine);

        Display d=ConcurrentStudent.this.getWindowManager().getDefaultDisplay();
        /*获取布局参数*/
        i=li.getLayoutParams();
        i.height=d.getHeight()/4;
        i.width=d.getWidth();

        areaArrayItems = getResources().getStringArray(R.array.area_array);
        typeItems = getResources().getStringArray(R.array.type);
        areaItems = getResources().getStringArray(R.array.area);
        request.setOnClickListener(this);
        back.setOnClickListener(this);

        enterprise.setOnClickListener(this);
        mine.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        switch (v.getId()){
            case R.id.request:
               /* intent.setClass(ConcurrentStudent.this,JobRequirement.class);
                startActivity(intent);*/
                break;
            case R.id.back:
                finish();
                break;
            case R.id.enterprise:
             //   Toast.makeText(ConcurrentStudent.this, "您点击的是ent", Toast.LENGTH_SHORT).show();
                break;
            case R.id.mine:
                Intent in=new Intent(context,CenterActivity.class);
                startActivity(in);
                break;
            default:break;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        mClear();
        getLv();
    }
    private void mClear() {
        listAll.clear();
        listSize=0;
        pagenum=1;
        isButtom=false;
    }
    public void getLv() {
        Retrofit retrofit= new GeneralFun().gsonCreate();
        CampusShopApi api=retrofit.create(CampusShopApi.class);
        Call< List<CampusEnterpriseListBean>> call=api.getEnterpriseList(pagenum);
        call.enqueue(new Callback< List<CampusEnterpriseListBean>>() {
            @Override
            public void onResponse(Call< List<CampusEnterpriseListBean>> call, Response< List<CampusEnterpriseListBean>> response) {
                if(response.isSuccessful()){
                    List<CampusEnterpriseListBean> list=response.body();
                    if(list.size()!=0) {
                        listAll.addAll(list);
                        listSize = listAll.size() - 1;
                        adapter.setList(listAll);

                        if(list.size()!=maxItem)
                            isButtom=true;
                    }else{
                        isButtom=true;
                    }
                }

            }

            @Override
            public void onFailure(Call< List<CampusEnterpriseListBean>> call, Throwable throwable) {
                // Toast.makeText(CampusShopAll.this.getActivity(),throwable.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });
    }
}
